package com.sky.controller.admin;

import com.sky.constant.MessageConstant;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.pojo.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.common.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 分类请求处理类
 * @author zgp
 * @version V1.0
 * @date 2025-01-13 19:40
 */
@RestController("adminCategoryController")
@RequestMapping("/admin/category")
@Slf4j
@Api(tags = "分类管理")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @GetMapping("/page")
    @ApiOperation("分类条件分页查询")
    public Result<PageResult<Category>> pageList(CategoryPageQueryDTO categoryPageQueryDTO){
        log.info("分类条件分页查询，categoryPageQueryDTO：{}", categoryPageQueryDTO);
        return Result.success(categoryService.getPageList(categoryPageQueryDTO));
    }

    @PostMapping
    @ApiOperation("分类新增")
    public Result<String> addCategory(@RequestBody CategoryDTO categoryDTO){
        log.info("分类新增，categoryDTO：{}", categoryDTO);
        categoryService.addCategory(categoryDTO);
        return Result.success(MessageConstant.ADD_SUCCESS);
    }

    @DeleteMapping
    @ApiOperation("分类删除")
    public Result<String> deleteCategory(Integer id){
        log.warn("分类删除，id：{}", id);
        categoryService.deleteCategory(id);
        return Result.success(MessageConstant.DELETE_SUCCESS);
    }

    @GetMapping("/list")
    @ApiOperation("根据类型查询分类")
    public Result<List<Category>> getCategoryByType(Integer type){
        log.info("根据类型查询分类，type：{}", type);
        return Result.success(categoryService.getCategoryByType(type));
    }

    @PostMapping("/status/{status}")
    @ApiOperation("启用/禁用分类")
    public Result<String> enableOrDisableCategory(@PathVariable Integer status, Long id){
        log.warn("启用/禁用分类，status：{}，id：{}", status, id);
        categoryService.enableOrDisableCategory(status, id);
        return Result.success(MessageConstant.UPDATE_SUCCESS);
    }

    @PutMapping
    @ApiOperation("分类修改")
    public Result<String> modifyCategory(@RequestBody CategoryDTO categoryDTO){
        log.info("分类修改，categoryDTO：{}", categoryDTO);
        categoryService.updateCategoryById(categoryDTO);
        return Result.success(MessageConstant.UPDATE_SUCCESS);
    }

}
